$(document).ready(function() {
	
	// Meldungen nach 3 Sekunden ausblenden
	$("#meldung").delay(3000).fadeOut(1000);
	
	// Alle Checkboxen marieren
	$("input:checkbox.checkAll").click(function(){
		var $this = $(this);
		var checkState = $this.attr("checked");
		if (checkState) !checkState;
		$this.closest("form").find("input:checkbox").attr("checked",checkState);
	});
	
	// Smilievorschau unter Einstellungen nachladen	
	var $smiliePackSelector =  $("select[name=smiliePack]");
	var currentSmiliePack = $smiliePackSelector.val();
	var somethingDifferent = false;
	$("select[name=smiliePack]").change(function() { if ($(this).val() != currentSmiliePack) somethingDifferent = true; });
	var $smilieMouseOverSection = $smiliePackSelector.next("div#smo");
	var $smilieDisplayBox = $smilieMouseOverSection.find("div#sm");	
	$smilieMouseOverSection.mouseover(function(e) {
		$smilieDisplayBox.show();
		if (somethingDifferent) {
			$smilieDisplayBox.html("");
			erstelleLadegrafik("../bilder/load.gif",$smilieDisplayBox[0]);
			sendeAnfrage("get","../news_funktionen.php?smilieRequest=" + $smiliePackSelector.val(),function(r){ $smilieDisplayBox.html(r);});
			somethingDifferent = false;
		}
	});
	
	$smilieMouseOverSection.mouseout(function() {
		$smilieDisplayBox.hide();
	});
	
});

/**
* Onload Events
*
*/
onload = function () {	
	
	if (document.templateBearbeiten) {
		var klapper = getElementsByClassName(document.getElementById("formArea"),"klapper");
		var crosses = getElementsByClassName(document.getElementById("formArea"),"cross");
		for (var i =0; i < klapper.length; i++ ) textareaHelperUmEventsAnreichern (klapper[i],crosses[i]);
	}
	
	var editTemplate = document.getElementById("editTemplate");
	if (editTemplate) {
		editTemplate.onclick = templateBearbeiten;
	}
	var formArea = document.getElementById("formArea");
	if (formArea) {
		var alreadyTemplates = getElementsByClassName(formArea,"front_");
		if (alreadyTemplates.length>0) {
			for (var i = 0;i < alreadyTemplates.length; i++) {
				codeMirrorFuerTextarea (alreadyTemplates[i],"xmlcolors.css","parsexml.js","200px");
			}
		}
	}
	
	
	// CSS Code-Highlighting Configurieren
	var  CodeMirror = {};
	if (CodeMirror != 'undefined') {
		if (document.getElementById("contenttemplate")) codeMirrorFuerTextarea ("contenttemplate","xmlcolors.css","parsexml.js");
		if (document.getElementById("contentcss")) codeMirrorFuerTextarea ("contentcss","csscolors.css","parsecss.js");		
	}
	
	
};

function templateBearbeiten() {

	var selector = document.getElementById("templateSelector");
	var formArea = document.getElementById("formArea");
	if (selector.innerHTML.indexOf("front")<0) return;	

	// Die offenen zuklappen
	var klapper = getElementsByClassName(formArea,"klapper");
	for (var i = 0; i < klapper.length; i++) {
		var kd = findeTextAreaHolder(klapper[i]); //klapper[i].parentNode.nextSibling;
		if (kd.className == "textAreaHolder ausgeklappt") {
			kd.className = "textAreaHolder eingeklappt";
			klapper[i].src = "../bilder/admin/plus.gif";
		} 
	}
	
	
	var template;
	for (var i = 0; selector.childNodes.length > i; i++) {
		var option = selector.childNodes[i];
		if (!option.tagName) continue;
		if (!option.selected) continue;
		template = option.value;
		selector.removeChild(option);
		break;
	}
	
	sendeAnfrage("get","../templates/" + template + ".tpl",function(r) { 
		var holderBox = document.createElement("DIV");
		holderBox.className = "holderBox";
		var holderTextArea = document.createElement("DIV");
		holderTextArea.className = "textAreaHolder ausgeklappt";
				
		var area = document.createElement("TEXTAREA");
		area.id = template;
		area.name = template;
		var zeilen = r.split("\n");
		
		area.value = r;		
		holderTextArea.appendChild(area);
		
		addTextareaHelper(holderBox,template);
		holderBox.appendChild(holderTextArea);
		formArea.appendChild(holderBox);
		
		
		var hoehe = (zeilen.length+3)*18;
		if (hoehe>500) hoehe = 500;
		
		var  CodeMirror = {};
		if (CodeMirror != 'undefined') codeMirrorFuerTextarea (area,"xmlcolors.css","parsexml.js",hoehe+"px");		

	});

}

function findeTextAreaHolder(elm) {
	for (var i = 0; i < elm.parentNode.parentNode.childNodes.length;i++) {
		var akt = elm.parentNode.parentNode.childNodes[i];
		if (akt.className && akt.className.indexOf("textAreaHolder")>-1) return akt;		
	}
	return false;
}

function addTextareaHelper(woran,template) {
	
	var box = document.createElement("div");	
	box.className = "helperBox";
	
	var klapper = document.createElement("IMG");
	klapper.src = "../bilder/admin/minus.gif";
	klapper.alt = "klapper";
	klapper.className = "klapper";
	
	var cross = document.createElement("IMG");
	cross.src = "../bilder/admin/cross.png";
	cross.alt = "cross";
	cross.className = "cross";
	
	textareaHelperUmEventsAnreichern (klapper,cross);
	
	box.appendChild(klapper);
	box.appendChild(cross);
	
	var bez = document.createElement("SPAN");
	bez.innerHTML = template;
	box.appendChild(bez);

	woran.appendChild(box);
}

function textareaHelperUmEventsAnreichern (klapper,cross){

	klapper.onclick = function (e) {
		var kd = findeTextAreaHolder(klapper);
		if (kd.className == "textAreaHolder ausgeklappt") {
			kd.className = "textAreaHolder eingeklappt";
			klapper.src = "../bilder/admin/plus.gif";
		} else {
			kd.className = "textAreaHolder ausgeklappt";
			klapper.src = "../bilder/admin/minus.gif";			
		}
	};

	cross.onclick = function (e) {		
		var selector = document.getElementById("templateSelector");
		var span = nextWhileTagName(e.target,"SPAN");
		var tpl = span.innerHTML;
		if (selector.innerHTML.indexOf(tpl)>-1) return;
		var o = document.createElement("option");	
		o.innerHTML = tpl;
		selector.appendChild(o);
		e.target.parentNode.parentNode.parentNode.removeChild(e.target.parentNode.parentNode);
	};
}

function nextWhileTagName(elm,tn){
	if (!elm.tagName || elm.tagName.indexOf(tn.toUpperCase())<0) return nextWhileTagName(elm.nextSibling,tn);	
	else return elm;
}

/**
* @return Object Dom Tree
* @url http://www.pedrocorreia.net/mySnippets/javascript/getElementsByClassName-Walking-Down-DOM
*/
var getElementsByClassName = function(start_node,cls) {
	var _elems = [];
	(function(start_node) {
		if (!start_node) { start_node = document.body; }
		var node = "";

		for (var i = 0, count = start_node.childNodes.length; i < count; i++) {
			node = start_node.childNodes[i];
			if (node.hasChildNodes()) arguments.callee(node); //recursive call
			if (node.className && node.className.indexOf(cls) >= 0) _elems.push(node);
		}
	})();
	return _elems;
};



/**
* Wandelt einen normalen Textarea zu einem mit highliting um
* @param name HTML-Element oder Textarea Name auf den die Funktion angewendet werden soll
* @param highlightingCSS CSS-Datei für highlighting
* @param highlightingJS Javascript Parser Regeln für highlighting
* @param heightp (optional) höhe des Editor Areas 
*
*/

function codeMirrorFuerTextarea (name,highlightingCSS,highlightingJS,heightp) {
	if (!heightp) var heightp = "500px";	
		CodeMirror.fromTextArea(name, {
				height: heightp,
				parserfile: highlightingJS,
				iframeClass: name,
				lineNumbers: true,
				stylesheet: "../js/CodeMirror/css/"+highlightingCSS,
				path: "../js/CodeMirror/js/"
		});
}


// Passwort ein- und ausblenden
function passwort () {
	var kasten = document.getElementById('adminpass');
	if (kasten.style.display != 'block') {
		kasten.style.display = 'block';
	} else {
		kasten.style.display = 'none';
	}
}

// Checkboxen beim "Kommentare löschen" alle markieren oder demarkieren
function selectAll(form) {
	for (var i = 0;i < form.elements.length;i++) {
		if(form.elements[i].type=="checkbox"){
			form.elements[i].checked = form.alle.checked;
		}
	}
}

